package base.regex.demo01;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zjw
 * @date 2022/3/25 15:39
 * @des java中使用正则表达式
 */
public class TestDemo2 {

    /**
     * Pattern 对象是一个正则表达式的编译表示。
     * Matcher 对象是对输入字符串进行解释和匹配操作的引擎。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
     */
    @Test
    public void testCase1(){
        String content = "I am noob from runoob.com.";
        String regex = ".*runoob.*";
        boolean isMatch = Pattern.matches(regex, content);
        System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
    }

    /**
     *
     */
    @Test
    public void testCase2(){
        String line = "This order was placed for QT3000! OK?";
        String regex = "(\\D*)(\\d+)(.*)";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(line);

        System.out.println("group(0)它总是代表整个表达式。groupCount()的返回值不包括该组: " + matcher.groupCount());
        if (matcher.find()) {
            System.out.println("Found value0: " + matcher.group(0) );
            System.out.println("Found value1: " + matcher.group(1) );
            System.out.println("Found value2: " + matcher.group(2) );
            System.out.println("Found value3: " + matcher.group(3) );
//            System.out.println("Found value4: " + matcher.group(4) );//No group 4
        } else {
            System.out.println("NO MATCH");
        }
    }

    /**
     * 在其他语言中，\\ 表示：我想要在正则表达式中插入一个普通的（字面上的）反斜杠，请不要给它任何特殊的意义。
     *
     * 在 Java 中，\\ 表示：我要插入一个正则表达式的反斜线，所以其后的字符具有特殊的意义。
     */
    @Test
    public void testCase3(){
        String regex = "\\bcat\\b";
        String str = "cat cat cat cattie cat";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        int count = 0;

        while (matcher.find()) {
            count++;
            System.out.println("Match number "+ count);
            System.out.println("start: "+ matcher.start());
            System.out.println("end: "+ matcher.end());
        }
    }

    /**
     * matches 和 lookingAt 方法都是将字符串与模式匹配。
     * 它们的不同是 matches 要求整个字符串都匹配，而lookingAt 只要求字符串中部分匹配上模式即可。
     *
     * lookingAt 方法虽然不需要整句都匹配，但是需要从字符起始位置开始匹配。
     */
    @Test
    public void testCase4(){
        String regex = "foo";
        String s1 = "fooooooooooooooooo";
        String s2 = "ooooofoooooooooooo";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s1);
        Matcher matcher2 = pattern.matcher(s2);

        System.out.println("1: " + matcher.lookingAt());//true    s1中以foo开头
        System.out.println("2: " + matcher.matches());//false     s1不全匹配foo
        System.out.println("3: " + matcher2.lookingAt());//false  s2中不以foo开头
        System.out.println("4: " + matcher2.matches());//false    s2不全匹配foo
    }

    @Test
    public void testCase5(){
        String s = "The dog says meow. All dogs say meow.";
        String regex = "dog";
        String replacement = "cat";
        String s1 = s.replaceAll(regex, replacement);
        System.out.println(s1);

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);
        String s2 = matcher.replaceAll(replacement);
        System.out.println(s2);
    }

    @Test
    public void testCase6(){
        String regex = "a*b";
        String s = "aabfooaabfooabfoobkkkb";
        String replacement = "-";
        StringBuffer sb = new StringBuffer();

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);
//        System.out.println(matcher.group(0));//No match found
//        while (matcher.find()){
//            System.out.println(matcher.group());
//        }
        while (matcher.find()) {
            matcher.appendReplacement(sb, replacement);
        }
        matcher.appendTail(sb);
        System.out.println(sb.toString());
        System.out.println(s);
    }


}
